//
//  HttpClient.m
//  FaceUpVersion
//
//  Created by jiahua on 15/7/24.
//  Copyright (c) 2015年 secsmarts. All rights reserved.
//

#import "HttpClient.h"
#import "sstNSDataFormat.h"

static NSString *HOST=nil;

@implementation HttpClient

+(NSString *)HOST{
    if(HOST == nil){
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
        NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
        HOST = [data objectForKey:@"HostURL"];
    }
    return HOST;
}

+(void)setHOST:(NSString *)host{
    HOST = host;
}

+(NSString*)makeGetUrlBySession:(NSString*)sess_id withParamOpt:(NSString*)opt{
    return [NSString stringWithFormat:@"%@/getSessionKey?%@%@%@%@",[HttpClient HOST],@"sess_id=", sess_id, @"&opt=", opt];
}

+(NSString*)makeGetUrlBySessionAndUdpId:(NSString*)sess_id withUdpId:(NSString*)udpId{
    NSString* url = [NSString stringWithFormat:@"%@/getToken?%@%@%@%@",[HttpClient HOST],@"sess_id=", sess_id, @"&udpid=", udpId];
   // NSLog(@"this is the url --> %@", url);
    return url;
}

+(NSData *)httpGet:(NSString *)UrlString
{
    NSLog(@"httpGet urlString: %@",UrlString);
    NSURL * url = [NSURL URLWithString:[UrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url];
    NSURLResponse * response = nil;
    NSError * error = nil;
    NSData * backData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error) {
        NSLog(@"error: %@",[error localizedDescription]);
    }else{
       // NSLog(@"response : %@",response);
       // NSLog(@"backData : %@",[[NSString alloc]initWithData:backData encoding:NSUTF8StringEncoding]);
    }
    return backData;
}

+(NSData *)httpPost:(NSString *)UrlString withParam:(NSString *)param
{
    //转化两个数据
    NSURL * url = [NSURL URLWithString:[UrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSData * data = [param dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc]init];
    [request setHTTPMethod:@"get"]; //指定请求方式
    [request setTimeoutInterval:5];//设置请求超时时间为5s
    [request setURL:url]; //设置请求的地址
    [request setHTTPBody:data];  //设置请求的参数
    
    NSURLResponse * response;
    NSError * error;
    NSData * backData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    if (error) {
        NSLog(@"error : %@",[error localizedDescription]);
    }else{
        NSLog(@"http response statusCode: %ld", (long)[(NSHTTPURLResponse*)response statusCode]);
        NSLog(@"response : %@",response);
        NSLog(@"backData : %@",[sstNSDataFormat NSData2NSString:backData]);
    }
    return backData;
}


+(NSData *)getTokenBySessionAndUdpid: (NSString*)sessid withUdpId:(NSString*)udpId
{
    return [HttpClient httpGet: [HttpClient makeGetUrlBySessionAndUdpId: sessid withUdpId:udpId]];
}
@end


